package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 壮哥
 * @Date 2021/9/19/19:49
 */
@Api(value = "频道管理API接口",tags = "频道管理API接口",description = "管理频道相关接口")
@RestController
@RequestMapping("/api/v1/channel")
public class AdChannelController {
    @Autowired
    private AdChannelService channelService;
    @ApiOperation(value="分页查询频道列表",notes = "按照name模糊查询,分页查询 按照ord升序")
    @PostMapping("/list")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDTO dto){
        return channelService.findByNameAndPage(dto);
    }
    @ApiOperation("频道新增")
    @PostMapping("/save")
    public ResponseResult insert(@RequestBody AdChannel channel){
        return channelService.insert(channel);
    }

    @ApiOperation("频道修改")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AdChannel channel){
        return channelService.update(channel);
    }

    @ApiOperation("根据id删除")
    @GetMapping("/delete/{id}")
    public ResponseResult deleteById(@PathVariable("id") Integer id){
        if (id==1){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID,"id不可以为1");
        }
        return channelService.deleteById(id);
    }

    @ApiOperation("查看全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll(){
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }
    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id) {
        return ResponseResult.okResult(channelService.getById(id));
    }
}
